﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public class Result
    {
        public int Code { get; set; }

        public static Result Ok()
        {
            return new Result() { Code = 0 };
        }

        public static Result<T> Ok<T>(T data)
        {
            return new Result<T>() { Code = 0, Data = data };
        }

        public static ResultList<T> Ok<T>(IEnumerable<T> data, int count)
        {
            return new ResultList<T>() { Code = 0, Data = data?.ToArray() ?? new T[0], Count = count };
        }
        public static ResultError Fail(string message, Enum code)
        {
            return new ResultError() { Message = message, Code = code.GetHashCode() };
        }
        public static ResultError Fail(string message, int code)
        {
            return new ResultError() { Message = message, Code = code };
        }

        public static ResultError Fail(string message, int code, IDictionary<string, string> errors)
        {
            return new ResultErrors() { Message = message, Code = code, Errors = errors };
        }
    }
    public class Result<T> : Result
    {
        public T Data { get; set; }
    }

    public class ResultList<T> : Result
    {
        /// <summary>
        /// 分页总条数
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        public T[] Data { get; set; }

    }
    public class ResultError : Result
    {
        public string Message { get; set; }
    }

    public class ResultErrors : ResultError
    {
        public IDictionary<string, string> Errors { get; set; }
    }
}
